<template>
  <div>
    <el-dialog
      v-el-drag-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
        <el-form-item label="专题名称" prop="">
          <el-select v-model="dataForm.hotType" placeholder="热词类型">
            <el-option
            v-for="type in dataForm.hotTypeOption"
            :key="type.value"
            :label="type.label"
            :value="type.value">
            </el-option>
	      </el-select>
        </el-form-item>
        <el-form-item label="选择商品">
          <el-button @click="addOrUpdateHandle">选择商品</el-button>
          <el-table v-if="goodsList.length !== 0"
            :data="goodsInfoList"
            border
            style="width: 100%">
            <el-table-column
              prop="img"
              label="商品图">
              <template slot-scope="scope">
                <el-popover
                  placement="right"
                  trigger="hover">
                  <img :src="scope.row.img" style="max-height: 100px;"/>
                  <img slot="reference" :src="scope.row.img" style="max-height: 50px;max-width: 130px">
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
              prop="goodsName"
              label="商品名">
            </el-table-column>
            <el-table-column
              prop="address"
              label="价格类型">
              <template slot-scope="scope">
                {{scope.row.address === 1 ?'兑换商城':'福利商城'}}
              </template>
            </el-table-column>
            <el-table-column
              prop="goodsInventory"
              label="库存">
            </el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              label="操作">
              <template slot-scope="scope">
                <el-button type="text" size="small" v-if="isAuth('home:floor:remove')"  @click="deleteGoodsHandle(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @getGoodsList="getGoodsList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './recommend-goods'
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        addOrUpdateVisible: false,
        goodsList: [],
        goodsInfoList: [],
        dataForm: {
          id: 0,
          remark: '',
          floorName: '',
          seq: 0
        },
        dataRule: {}
      }
    },
    components: {
      AddOrUpdate
    },
    methods: {
      getGoodsList (goodsList, goodsInfoList) {
        this.goodsList = goodsList
        this.goodsInfoList = goodsInfoList
      },
      init (id) {
        Object.assign(this.$data, this.$options.data())
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/home/floor/getFloorById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.remark = data.data.remark
                this.dataForm.floorName = data.data.floorName
                this.goodsList = []
                this.goodsInfoList = []
                let than = this
                data.data.urlData.split(',').map(
                  item => {
                    let id = Number.parseInt(item)
                    if (Number.isInteger(id)) {
                      this.getGoods(id).then(data => {
                        than.goodsList.push(id)
                        than.goodsInfoList.push({
                          'img': data.img,
                          'id': data.id,
                          'goodsName': data.goodsName,
                          'priceType': data.priceType,
                          'goodsInventory': data.goodsInventory
                        })
                      })
                    }
                  })
                this.dataForm.seq = data.data.seq
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit: function () {
        if (this.goodsList.length > 4) {
          this.$message.error('最多只能添加20件商品')
          return false
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/special/${!this.dataForm.id ? 'addSpecialDetails' : 'modifySpecialDetails'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorName': this.dataForm.floorName,
                'seq': this.dataForm.seq,
                'urlData': this.goodsList.join(','),
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(this.goodsList, this.goodsInfoList)
        })
      },
      deleteGoodsHandle (id) {
        this.goodsList.splice(this.goodsList.indexOf(id), 1)
        for (let i = 0; i < this.goodsInfoList.length; i++) {
          if (this.goodsInfoList[i].id === id) {
            this.goodsInfoList.splice(i, 1)
            i--
          }
        }
      },
      async getGoods (id) {
        let info
        try {
          await this.$http({
            url: this.$http.adornUrl(`/goods/getGoodsById/${id}`),
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              info = data.data
            }
          })
        } catch (e) {
          console.log(e)
        }
        return info
      }
    }
  }
</script>
